import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";

export default ({ mode }: { mode: string }) => {
    // 加载所有环境变量（包含 VITE_ 开头的变量）
	const env = loadEnv(mode, process.cwd(), ['VITE_', 'NODE_ENV']);

    // 配置代理
    const isEncrypt = env.VITE_IS_ENCRYPT; // 是否加密
    const port = parseInt(env.VITE_PORT); // 端口号
    const target = env.VITE_TARGET; // 代理地址
    const Paths = ["/api", "/emp", "/auth", "/evaluation", "/master", "/system", "/emp-system", "/usermanager", "/resource"]; // 代理路径

    // 代理配置
    const proxy = Paths.reduce<{ [key: string]: string }>((obj, path) => {
        obj[path] = target;
        return obj;
    }, {});

    // 服务器配置
    const server: any = {
        host: "0.0.0.0",
        port,
        proxy,
        https: false,
    };

    // 打印配置信息
	console.log(`当前环境：${mode}`);
	console.log(`端 口 号：${port}`);
	console.log(`是否加密：${isEncrypt}`);
	console.log(`代理地址：${target}`);
	console.log(`代理路径：${Paths}`);
	console.log(`代理配置：${JSON.stringify(proxy, null, 4)}`);
	console.log(`服务器配置：${JSON.stringify(server, null, 4)}`);
	console.log(`环境变量：${JSON.stringify(env, null, 4)}`);
	console.log("------------------------------------------------------------");

	return defineConfig({
		base: "./", //打包路径，也可以写成base: '/your-project-name/'
		plugins: [vue()],
		server,
	});
};
